<template>
  <view v-if="visible" class="modal-overlay" @click="closeModal">
    <view class="modal-container" @click.stop>
      <view class="modal-header">
        <text class="modal-title">{{ currentDevice.name }}</text>
        <view class="close-btn" @click="closeModal">×</view>
      </view>
      
      <view class="modal-content">
        <view class="info-section">
          <view class="info-item">
            <text class="info-label">📍 位置</text>
            <text class="info-value">{{ currentDevice.location }}</text>
          </view>
          
          <view class="info-item">
            <text class="info-label">📊 状态</text>
            <view class="status-container">
              <view class="status-dot" :class="currentDevice.status"></view>
              <text class="status-text">{{ getStatusText(currentDevice.status) }}</text>
            </view>
          </view>
        </view>
        
        <view class="data-section">
          <text class="section-title">水质数据</text>
          <view class="data-grid">
            <view class="data-item" :class="{ 'fault-item': isPhFault(currentDevice.ph) }">
              <text class="data-label">pH值</text>
              <text class="data-value">{{ currentDevice.ph }}</text>
            </view>
            <view class="data-item" :class="{ 'fault-item': isTempFault(currentDevice.temp) }">
              <text class="data-label">温度</text>
              <text class="data-value">{{ currentDevice.temp }}°C</text>
            </view>
            <view class="data-item" :class="{ 'fault-item': isNtuFault(currentDevice.ntu) }">
              <text class="data-label">浊度</text>
              <text class="data-value">{{ currentDevice.ntu }} NTU</text>
            </view>
          </view>
        </view>
        
        <view class="time-section" v-if="currentDevice.timestamp">
          <text class="time-label">更新时间</text>
          <text class="time-value">{{ formatTime(currentDevice.timestamp) }}</text>
        </view>
      </view>
      
      <view class="modal-footer">
        <button class="confirm-btn" @click="closeModal">确定</button>
      </view>
    </view>
  </view>
</template>

<script>
// 正常范围定义（与地图页面保持一致）
const NORMAL_RANGE = {
  ph: { min: 6.5, max: 8.5 },
  temp: { min: 20, max: 30 },
  ntu: { min: 0, max: 3000 }
}

export default {
  name: 'DeviceInfoModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    device: {
      type: Object,
      default: () => ({})
    }
  },
  computed: {
    // 响应式计算属性，当device数据变化时自动更新
    currentDevice() {
      return this.device || {}
    }
  },
  watch: {
    // 监听device数据变化
    device: {
      handler(newDevice) {
        if (newDevice && this.visible) {
          // 当设备数据变化时，强制更新组件
          this.$forceUpdate()
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    closeModal() {
      this.$emit('close')
    },
    getStatusText(status) {
      const statusMap = {
        'online': '在线',
        'offline': '离线', 
        'fault': '故障'
      }
      return statusMap[status] || status
    },
    formatTime(timestamp) {
      if (!timestamp) return ''
      const date = new Date(timestamp)
      return date.toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      })
    },
    // 判断单个数据项是否异常
    isPhFault(ph) {
      return ph < NORMAL_RANGE.ph.min || ph > NORMAL_RANGE.ph.max
    },
    isTempFault(temp) {
      return temp < NORMAL_RANGE.temp.min || temp > NORMAL_RANGE.temp.max
    },
    isNtuFault(ntu) {
      return ntu < NORMAL_RANGE.ntu.min || ntu > NORMAL_RANGE.ntu.max
    }
  }
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.modal-container {
  background: white;
  border-radius: 12px;
  width: 320px;
  max-width: 90vw;
  max-height: 80vh;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, #4CAF50, #45a049);
  color: white;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  cursor: pointer;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
}

.modal-content {
  padding: 20px;
}

.info-section {
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-label {
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.info-value {
  font-size: 14px;
  color: #333;
  flex: 1;
  text-align: right;
  margin-left: 10px;
}

.status-container {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.status-dot.online {
  background: #4CAF50;
}

.status-dot.offline {
  background: #9E9E9E;
}

.status-dot.fault {
  background: #F44336;
}

.status-text {
  font-size: 14px;
  color: #333;
}

.data-section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 12px;
  display: block;
}

.data-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
}

.data-item {
  text-align: center;
  padding: 12px 8px;
  background: #f8f9fa;
  border-radius: 8px;
  border: 1px solid #e9ecef;
}

.data-label {
  display: block;
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.data-value {
  display: block;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

/* 异常数据项样式 */
.fault-item {
  background: #ffebee !important;
  border: 2px solid #f44336 !important;
  animation: fault-pulse 2s infinite;
}

.fault-item .data-label {
  color: #d32f2f !important;
  font-weight: 600;
}

.fault-item .data-value {
  color: #d32f2f !important;
  font-weight: 700;
}

/* 故障脉冲动画 */
@keyframes fault-pulse {
  0%, 100% { 
    box-shadow: 0 0 0 0 rgba(244, 67, 54, 0.4);
  }
  50% { 
    box-shadow: 0 0 0 8px rgba(244, 67, 54, 0.1);
  }
}

.time-section {
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  text-align: center;
}

.time-label {
  display: block;
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.time-value {
  display: block;
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.modal-footer {
  padding: 16px 20px;
  background: #f8f9fa;
  border-top: 1px solid #e9ecef;
}

.confirm-btn {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #4CAF50, #45a049);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.confirm-btn:active {
  background: linear-gradient(135deg, #45a049, #3d8b40);
}
</style>

